---
title: 'Figure 3'
output:
  html_document:
    toc: true
---

```{r setup, include=FALSE}
library(ISLR)
library(leaps)
library(ggplot2)
library(glmnet)
library(corrplot)
knitr::opts_chunk$set(echo = TRUE)
```

```{r problem1, eval=TRUE}
#Step 1: load data and log transform data
setwd("./")
Machine.Dat <- read.table ("combine_name_freq2param_max_min_ss.dat",sep = " ")
colnames(Machine.Dat)=c("id","factor","power","err","DMAX","DMIN","p_H","p_B","p_E","p_G","p_I","p_T","p_S","p_~")
write.table(Machine.Dat,file="./week4.data.tsv",sep="\t",col.names = T,row.names = F,quote = F)
logCPU=log(Machine.Dat[,c(2,5:14)]+1)
M=cor(logCPU,use="pairwise", method="pearson")
corrplot(M, method="color")
# STEP 2: Select the best subsets of variables for predicting PRP
summaryMetrics <- NULL
whichAll <- list()
##for ( myMthd in c("exhaustive", "backward", "forward", "seqrep") ) {
for ( myMthd in c("backward") ) {
  rsRes <- regsubsets(factor~.,logCPU,method=myMthd,nvmax=10)
  summRes <- summary(rsRes)
  whichAll[[myMthd]] <- summRes$which
#  for ( metricName in c("rsq","rss","adjr2","cp","bic") ) {
  for ( metricName in c("rsq") ) {
    summaryMetrics <- rbind(summaryMetrics,
      data.frame(method=myMthd,metric=metricName,
                nvars=1:length(summRes[[metricName]]),
                value=summRes[[metricName]]))
  }
}
ggplot(summaryMetrics,aes(x=nvars,y=value,shape=method,colour=method)) + geom_path() + geom_point() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"), axis.title = element_text(size=18), axis.text  = element_text(size=18))+ scale_x_continuous(name="Num_variables", limits=c(0.0,11), breaks=seq(0,11,2), expand = c(0, 0))+ scale_y_continuous(name="Rsq", limits=c(0.45,0.6), expand = c(0, 0))+geom_line(size=2)+geom_point(colour = "black", size = 6)
# STEP 3: 
old.par <- par(mfrow=c(1,1),ps=15,mar=c(5,7,2,1))
for ( myMthd in names(whichAll) ) {
  image(1:nrow(whichAll[[myMthd]]),
        1:ncol(whichAll[[myMthd]]),
        whichAll[[myMthd]],xlab="Num_variables",ylab="",
        xaxt="n",yaxt="n",breaks=c(-0.5,0.5,1.5),
        col=c("white","gray"))
  axis(1,1:nrow(whichAll[[myMthd]]),rownames(whichAll[[myMthd]]))
  axis(2,1:ncol(whichAll[[myMthd]]),colnames(whichAll[[myMthd]]),las=2)
}
par(old.par)
```